<?php

/**
 * Name: 风险源模型层
 * User: Silent
 * Date: 2025-09-20
 * Time: 22:45:07
 */

namespace App\Models\Safety;

use App\Models\Config\Employee;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class RiskSource extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'risk_sources';

    protected $guarded = [];

    protected $casts = [
        'accident_type' => 'json',
        'created_at'    => 'datetime:Y-m-d H:i:s',
        'updated_at'    => 'datetime:Y-m-d H:i:s',
        'deleted_at'    => 'datetime:Y-m-d H:i:s',
    ];

    protected static $logName = 'risk_source';

    protected static function boot()
    {
        parent::boot();
        self::creating(function (self $model) {
            $model->status = 'pending';
        });
    }

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     *  getFormatAccidentTypeAttribute.
     *
     * @return string
     */
    public function getFormatAccidentTypeAttribute(): string
    {
        $str = [];

        if (empty($this->accident_type)) {
            return '';
        }

        foreach ($this->accident_type as $item) {
            $str[] = $item['value'];
        }

        return implode('、', $str);
    }

    /**
     * 格式化状态
     *
     * @return string
     */
    public function getFormatStatusAttribute(): string
    {
        switch ($this->status) {
            case 'pending':
                return '待处理';
            case 'processing':
                return '处理中';
            case 'rectification':
                return '暂时处理';
            case 'resolved':
                return '已处理';
            case 'reject':
                return '驳回';
            default:
                return '-';
        }
    }

    /**
     * 用户.
     */
    public function employees()
    {
        return $this->belongsTo(Employee::class, 'employees_id');
    }

}
